package com.zn.task;

import com.netflix.discovery.converters.Auto;
import com.zn.entity.AppCustomer;
import com.zn.entity.CustomerWithdraw;
import com.zn.mapper.AppCustomerMapper;
import com.zn.mapper.CustomerWithdrawMapper;
import com.zn.vo.Response;
import com.zn.web.rpc.PayRemoteCall;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created by yinjiawei on 2018/8/12.
 */
@Component
public class DfQueryTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(DfQueryTask.class);
    @Autowired
    private CustomerWithdrawMapper customerWithdrawMapper;
    @Autowired
    private PayRemoteCall payRemoteCall;
    @Autowired
    private AppCustomerMapper appCustomerMapper;

    @Scheduled(cron="0 0 4 * * ?")
    public void execute(){
        LOGGER.info("Start DfQueryTask................");
        CustomerWithdraw customerWithdraw=new CustomerWithdraw();
        customerWithdraw.setAuditStatus(1);
        customerWithdraw.setWithdrawStatus(2);
        List<CustomerWithdraw> customerWithdrawList=customerWithdrawMapper.selectBySqlWhere(customerWithdraw);
        if(customerWithdrawList.size()>0 && customerWithdrawList!=null){
            for(CustomerWithdraw obj:customerWithdrawList){
                LOGGER.info("Handle DfQueryTask................" + obj.getCustomerId());
                Response response=payRemoteCall.dfQueryMethod(obj.getWithdrawOrderNum());
                AppCustomer appCustomer=new AppCustomer();
                appCustomer.setCustomerId(obj.getCustomerId());
                /**0代付成功|1代付失败|2代付处理中(处理中的 不予处理)**/
                if("0".equals(response.getResultFlag())){
                    //出金成功 扣除客户总资金 撤销客户冻结资金 订单结束
                    appCustomer.setCustomerCapital(-obj.getWithdrawAmount());
                    appCustomer.setCustomerFreezeCapital(-obj.getWithdrawAmount());
                    appCustomerMapper.refreshCapital(appCustomer);
                    obj.setWithdrawStatus(Integer.valueOf(response.getResultFlag()));
                    obj.setResultMessage(response.getResultMessage());
                    obj.setWithdrawTime(new Date());
                    customerWithdrawMapper.updateByPrimaryKeySelective(obj);
                }
                if("1".equals(response.getResultFlag())){
                    //出金失败 撤销冻结资金 订单结束
                    appCustomer.setCustomerFreezeCapital(-obj.getWithdrawAmount());
                    appCustomerMapper.refreshCapital(appCustomer);
                    obj.setWithdrawStatus(Integer.valueOf(response.getResultFlag()));
                    obj.setResultMessage(response.getResultMessage());
                    customerWithdrawMapper.updateByPrimaryKeySelective(obj);
                }
            }
        }
        LOGGER.info("End DfQueryTask.................");
    }
}
